//
//  TableViewCellChat.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/18.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class TableViewCellChat: UITableViewCell {
    
    private let ID = "tableview_cell_chat_id"
    
    var timeView:UILabel?
    var textView:UIButton?
    var iconView:UIImageView?
    var imgShowView:UIImageView?
    
    var chatFrame:ChatFrame?{
        didSet {
            //设置cell的data
            setDataToView()
            //设置cell的frame
            setValueToView()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    func tableViewCellChat(_ tableView: UITableView) -> TableViewCellChat {
        var cell = tableView.dequeueReusableCell(withIdentifier:ID )
        if cell == nil {
            cell = TableViewCellChat(style: .default, reuseIdentifier: ID)
        }
        return cell as! TableViewCellChat
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 背景颜色
        backgroundColor = UIColor(displayP3Red: 240/250.0, green: 240/250.0, blue: 240/250.0, alpha: 1.0)
        // 创建子控件
        createView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 创建子控件
    func createView() -> Void {
        // 时间
        let label = UILabel()
        timeView = label
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 12)
        contentView.addSubview(label)
        // 头像
        let img = UIImageView()
        img.layer.cornerRadius = MARGIN50 * 0.5
        img.layer.masksToBounds = true
        iconView = img
        contentView.addSubview(img)
        // 正文
        let btn = UIButton()
        textView = btn
        btn.titleLabel?.numberOfLines = 0
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        btn.contentEdgeInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        contentView.addSubview(btn)
        // 图片
        let imgShow = UIImageView()
        imgShowView = imgShow
        imgShow.contentMode = .scaleAspectFill
        contentView.addSubview(imgShow)
    }
}

extension TableViewCellChat {
    
    func setDataToView() -> Void {
        let bean = chatFrame?.chatBean
        // 时间
        timeView?.text = bean?.time
        // 头像
        var imgIcon:UIImage?
        if let type = bean?.type, type.elementsEqual("0") {
            imgIcon = UIImage(named: "main_avatar_97x97")
        }else {
            imgIcon = UIImage(named: "other")
        }
        iconView?.image = imgIcon
        // 正文
        var normalImg,checkImg:UIImage?
        if (bean?.type?.elementsEqual("0"))! {
            normalImg = UIImage(named: "chat_send_nor")?.resizeImg()
            checkImg = UIImage(named: "chat_send_press_pic")?.resizeImg()
        }else {
            normalImg = UIImage(named: "chat_recive_nor")?.resizeImg()
            checkImg = UIImage(named: "chat_recive_press_pic")?.resizeImg()
        }
        textView?.setTitleColor(UIColor.black, for: .normal)
        textView?.setBackgroundImage(normalImg, for: .normal)
        textView?.setBackgroundImage(checkImg, for: .highlighted)
        // 正文 文字
        if bean?.picture != nil {
            textView?.setBackgroundImage(nil, for: .normal)
            textView?.setBackgroundImage(nil, for: .highlighted)
            textView?.contentMode = .center
            textView?.setImage(bean?.picture, for: .normal)
            //            imgShowView?.frame = chatFrame!.textFrame!
            //            imgShowView?.image = bean?.picture
        }else {
            textView?.setTitle(bean?.text, for: .normal)
        }
    }
    
    func setValueToView() -> Void {
        timeView?.frame = chatFrame!.timeFrame!
        iconView?.frame = chatFrame!.iconFrame!
        textView?.frame = chatFrame!.textFrame!
        timeView?.isHidden = chatFrame!.chatBean!.isHidden
    }
}
